﻿using System;
using System.Runtime.Serialization;
using FireStarter.Base;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;

namespace FireStarterModeller.BO
{
    public class UIProp : BusinessObject, IOrderableObject, IModelElement, IMergableBo
    {
        public UIProp():this(null)
        {
//            new PropertyLink<string, string>
//                (this, "Property", "LabelText", value => StringUtilities.DelimitPascalCase(Convert.ToString(value), " "));

            this.PendingChangeApproved = true;
            this.PendingChangeType = ChangeType.None;
        }

        #region Implementation of IMergableBo

        public ChangeType PendingChangeType { get; set; }

        public bool PendingChangeApproved
        {
            get { return true; }
            set { }
        }

        #endregion

        public virtual IModelElement Parent
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
        protected internal UIProp(IClassDef def) : base(def)
        {
        }

        protected UIProp(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public virtual Guid? UIPropID
        {
            get
            {
                return ((Guid?)(base.GetPropertyValue("UIPropID")));
            }
            set
            {
                base.SetPropertyValue("UIPropID", value);
            }
        }

        public virtual Boolean? Editable
        {
            get
            {
                return ((Boolean?)(base.GetPropertyValue("Editable")));
            }
            set
            {
                base.SetPropertyValue("Editable", value);
            }
        }

        public virtual String ToolTipText
        {
            get
            {
                return ((String)(base.GetPropertyValue("ToolTipText")));
            }
            set
            {
                base.SetPropertyValue("ToolTipText", value);
            }
        }

        public virtual Boolean? Generated
        {
            get
            {
                return ((Boolean?)(base.GetPropertyValue("Generated")));
            }
            set
            {
                base.SetPropertyValue("Generated", value);
            }
        }

        public virtual Int32? OrdinalPosition
        {
            get
            {
                return ((Int32?)(base.GetPropertyValue("OrdinalPosition")));
            }
            set
            {
                base.SetPropertyValue("OrdinalPosition", value);
            }
        }

        /// <summary>
        /// The PropertyName this can be 
        /// 1) a Reflective Property i.e. -PropertyName- this will allow the user interface to get or set the property value via Reflection.
        ///    This is typically used for derived value e.g. TotalValue = Value + VAT. but you want to show TotalValue in the Grid/Form.
        /// 2) A Related Property i.e. Customer.CustomerName this will allow the user interface to get or set the property value via the Property
        ///    of the related object e.g. the Order Form needs to show the customer Name that the order is associated with. The Related Property
        ///    can be n levels deep i.e. an Invoice Form can get a customer name as Order.Customer.CustomerName
        /// 3) An actual property i.e. the UIProp (User Interface Prop) is associated with a Property of the DMClass that is being displayed in the form.
        /// 4) A single relationship name i.e. the Customer Relationship may be mapped to a UIField for an Order Form this will result in a 
        ///    combo box (or similar control) being used to select the Customer for a Order. And the Customer.ToString being displayed when viewing.
        /// </summary>
        public virtual String PropertyName
        {
            get
            {
                return ((String)(base.GetPropertyValue("PropertyName")));
            }
            set
            {
                base.SetPropertyValue("PropertyName", value);
            }
        }
        public virtual  String UIPropType
        {
            get
            {
                return ((String)(base.GetPropertyValue("UIPropType")));               
            }
        }

        public virtual BusinessObjectCollection<UIParameter> UIParameters
        {
            get
            {
                return Relationships.GetRelatedCollection<UIParameter>("UIParameters");
            }
        }

        /// <summary>
        /// The DMProperty this UIProp is associated with.
        /// </summary>
        public virtual IDMProperty Property
        {
            get { return Relationships.GetRelatedObject<DMProperty>("Property"); }
            set { Relationships.SetRelatedObject("Property", value); }
        }
    }
}